<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

/**
 * 用户注销日志
 */
class UserCancelLog extends Model
{
    protected $table = 'user_cancellation_log';
    protected $fillable = ['user_id', 'flag'];

    //注销中
    const FLAG_ING = 0;
    //已注销
    const FLAG_OK = 1;
    //已撤销注销
    const FLAG_FAIL = 2;
    const FLAG = [
        self::FLAG_ING => '注销中',
        self::FLAG_FAIL => '已撤销',
        self::FLAG_OK => '已注销',
    ];

    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    /**
     * 撤销注销
     */
    static function cancelApply($userId = 0)
    {
        $item = self::where(['user_id' => $userId])->orderBy('id', 'desc')->first();
        if (!$item) return false;
        return $item->update(['flag' => self::FLAG_FAIL]);
    }

    /**
     * 确认注销
     */
    static function confirmApply($userId = 0)
    {
        $item = self::where(['user_id' => $userId])->orderBy('id', 'desc')->first();
        if (!$item) return false;
        return $item->update(['flag' => self::FLAG_OK]);
    }
}
